package com.app.miwawa.cui.system;

import java.util.List;
import java.util.Map;

import org.framework.common.api.support.model.PageModel;
import org.framework.common.api.support.model.QueryModel;
import org.framework.common.cui.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.app.miwawa.api.system.entity.Center;
import com.app.miwawa.api.system.service.bo.CenterBo;

/**
 * 早教中心控制器类
 * <p>描述: </p>
 * <p>Company: Professional</p>
 * @author chanlong(陈龙)
 * @date 2016年1月13日 下午3:30:30
 * @version 1.0.2016
 */
@RestController
public class CenterController extends AbstractController {

	@Autowired
	private CenterBo centerBo;
	
	@Autowired
	private JdbcTemplate jdbc;
	
	@RequestMapping("/system/center/save")
	public Object save(@ModelAttribute final Center entity) throws Exception {
		try{
			// 注册数据源
			registerDataSource(null);
			
			// 设置编码
			entity.setCode(centerBo.maxcode());
			
			// 保存实体对象
			return centerBo.insert(entity);
		} catch (Exception e) {
			e.printStackTrace();
			return error("保存实体对象失败！", e);
		}
	}
	
	@RequestMapping("/system/center/remove/{dbname}")
	public Object remove(@PathVariable final String dbname) throws Exception {
		try{
			// 注册数据源
			registerDataSource(null);
			
			// 删除实体对象
			centerBo.remove(dbname);
			
			return success("删除实体对象成功");
		} catch (Exception e) {
			e.printStackTrace();
			return error("删除实体对象失败！", e);
		}
	}
	
	@RequestMapping("/system/center/data/all.json")
	public Object dataAll(@ModelAttribute final QueryModel model) throws Exception {
		try{
			// 注册数据源
			registerDataSource(null);
			
			// 统计中心数据
			long count = jdbc.queryForObject("call pro_centerdata(0, 0, true)", Long.class);
			int limit = model.getLimit();
			int offset = model.getOffset();
			List<Map<String, Object>> result = jdbc.queryForList("call pro_centerdata(?, ?, false)", limit, offset);
			
			// 返回分页结果
			return new PageModel<Map<String, Object>>(count, result);
		} catch (Exception e) {
			return error("统计中心数据失败！", e);
		}
	}
}
